"""
基础的query处理基类
"""
import threading
from tool.string_tool import remove_punctuation


class BaseQueryAnalyse(threading.Thread):
    def __init__(self, config):
        super().__init__()
        self.config = config
        self.args = None

    def init(self, *args):
        self.args = args

    def process_query(self, global_resource, request_content, response_content):
        """
        处理完query后,将处理结果存入
        :return:
        """
        # 去掉标点符号
        raw_query = request_content.get_value("raw_query")
        clean_query = remove_punctuation(raw_query)
        request_content.store_value("clean_query", clean_query)

        # 分词
        terms = clean_query.split()
        request_content.store_value("terms", terms)

        # 通过用户的id获取用户的完整信息,将完整信息放入request_content中
        es_service = global_resource.resource_manager.get_service("es_client")
        body = {"query": {"term": {"user_id": request_content.get_value("user_id")}}}
        data_list = es_service.post_query("user", "user", body)
        if len(data_list) == 0:
            raise Exception("user not found in database")
        user = data_list[0]
        # 将需要的用户信息存入request_content
        request_content.store_value("follower_id_list", user["follower_id"])

    def run(self) -> None:
        if not self.args:
            print("query analyse service start without init")
            return
        self.process_query(*self.args)


